/*
 * AIRandom.cpp
 *
 *  Created on: 2011-5-5
 *      Author: Administrator
 */

#include "AIRandom.h"

using namespace Osp::Base::Utility;
using namespace Osp::Base::Collection;
using namespace Osp::Base;

namespace Galaxy {
AIRandom::AIRandom() {
	__unitsKeeped = (Math::Rand()%5)*5;
}

AIRandom::~AIRandom() {
}

void AIRandom::Play(GalaxyThread* pThread, Player* pMe,
		Osp::Base::Collection::ArrayList* pMyPlanets,
		Osp::Base::Collection::ArrayList* pOtherPlanets,
		Osp::Base::Collection::HashMap* pAccessiblePlanets){
	int myCount = pMyPlanets->GetCount();
	if(myCount == 0){
		return;
	}
	int otherCount = pOtherPlanets->GetCount();
	if(otherCount ==0){
		return;
	}
	int sourcePlanetIndex = Math::Rand()%myCount;
	Planet* pSourcePlanet = static_cast<Planet*>(pMyPlanets->GetAt(sourcePlanetIndex));
	if(pSourcePlanet->__soldiersCount < this->__unitsKeeped){
		return;
	}
	ArrayList* pAccessible = static_cast<ArrayList*>(pAccessiblePlanets->GetValue(*pSourcePlanet));
	int count = pAccessible->GetCount();
	if(count != 0){
		Planet* pDestPlanet = static_cast<Planet*>(pAccessible->GetAt(Math::Rand()%count));
		if(pDestPlanet->__pPlayer == NULL || pDestPlanet->__pPlayer->__team != pMe->__team){
			pThread->Attack(pSourcePlanet, pDestPlanet);
			return;
		}
	}
	int destPlanetIndex = Math::Rand()%otherCount;
	Planet* pDestPlanet = NULL;
	pDestPlanet = static_cast<Planet*>(pOtherPlanets->GetAt(destPlanetIndex));
	pThread->Attack(pSourcePlanet, pDestPlanet);
}
}

